package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.CooperativeMapper;
import com.heima.pojo.Cooperative;
import com.heima.pojo.dto.ReceiveDto;
import com.heima.pojo.vo.PagingVO;
import com.heima.pojo.vo.PolicyVO;
import com.heima.service.CooperativeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CooperativeServicelmpl implements CooperativeService {
    private static final Logger log = LoggerFactory.getLogger(CooperativeServicelmpl.class);
    @Autowired
    private CooperativeMapper cooperativeMapper;
    /**
     * @param dto
     * @return
     */
    @Override
    public PagingVO SelectCooperative(ReceiveDto dto) {
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page<Cooperative> cooperative = cooperativeMapper.SelectCooperative(dto.getName());
        PagingVO pagingVo = new PagingVO();
        pagingVo.setCurrentPageRecords(cooperative);
        pagingVo.setPageIndex(dto.getPageIndex());
        pagingVo.setPageSize(dto.getPageSize());

        pagingVo.setTotalCount( (int)cooperative.getTotal());
        pagingVo.setTotalPage(cooperative.getPageNum());


        log.info("分页查询合作方信息{}", pagingVo);


        return pagingVo;
    }

    /**
     * @param cooperative
     * @return
     */
    @Override
    public Boolean addCooperative(Cooperative cooperative) {
        log.info("新增合作商{}",cooperative);
        cooperative.setCreateTime(LocalDateTime.now());
        cooperative.setUpdateTime(LocalDateTime.now());
         return cooperativeMapper.addCooperative(cooperative);
    }

    /**获取合作商详情
     * @param id
     * @return
     */
    @Override
    public PolicyVO getCooperative(Long id) {

        return cooperativeMapper.getCooperative(id);
    }

    /**修改合作商
     * @param policyVo
     * @param id
     * @return
     */
    @Override
    public Boolean UpdateCooperative(PolicyVO policyVo, Long id) {
        return cooperativeMapper.UpdateCooperative(policyVo,id);

    }

    /**删除合作商
     * @param id
     * @return
     */
    @Override
    public Boolean deleteCooperative(Long id) {
        return cooperativeMapper.deleteCooperative(id);
    }
}
